package seotools.positions.direct;

import data.DataHelper;
import data.ExtramobiNew;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Number;
import obj.Item;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * User: Vladimir
 * Date: 08.12.12 Time: 9:33
 */
public class Tiu {
    private static Map<String, String> commonProps;

    public static void main(String[] args) throws IOException, BiffException, WriteException {
        commonProps = ExtramobiNew.readCommonProps();
        String path = "data/extramobi/accum/";
        List<Item> items = Item.parseItemsXlsDir(path);
        saveToXls("tiu.ru.xls", items, path + "accum.properties");
    }

    public static void saveToXls(String outPath, List<Item> items, String propsFile) throws IOException, WriteException {
        Map<String, String> categoryProps = DataHelper.readProps(DataHelper.Props.CATEGORY, propsFile);
        Map<String, String> templates = ExtramobiNew.createTemplateProps();
        File xlsFile = new File(outPath);
        WorkbookSettings s = new WorkbookSettings();
        s.setLocale(new Locale("ru", "RU"));
        WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, s);
        WritableSheet sheet = workbook.createSheet("List 1", 0);
        // print header
        int column = 0;
        int row = 0;
        WritableCell cell = new Label(column++, row, "Код_товара");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Название_позиции");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Ключевые_слова");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Краткое_описание");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Детальное_описание");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Цена");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Ссылка_изображения");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Адрес_подраздела");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Наличие");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Тип_товара");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Единица_измерения");
        sheet.addCell(cell);
        cell = new Label(column++, row, "Валюта");
        sheet.addCell(cell);
        for (Item item : items) {
            templates.put("\\$\\{brand\\.url\\}", item.brand);
            templates.put("\\$\\{brand\\.name\\}", item.brand);
            templates.put("\\$\\{item\\.name\\}", item.name);
            templates.put("\\$\\{model\\.shortlist\\}", ExtramobiNew.getShortList(item.models));
            templates.put("\\$\\{model\\.titlelist\\}", ExtramobiNew.getTitleList(item.models));
            Map<String, String> itemProps = DataHelper.readProps(DataHelper.Props.ITEM, propsFile);
            itemProps = ExtramobiNew.filterProps(itemProps, templates);
            ExtramobiNew.filterProp(itemProps, "item.name", "\\$\\{item\\.mah\\}", ExtramobiNew.getMah(item.props));
            column = 0;
            ++row;
            String categoryUrl;
            StringBuilder keywords = new StringBuilder(item.brand);
            if (categoryProps.get("category.engname").equalsIgnoreCase("accumulators")) {
                keywords.append(",").append("аккумулятор");
                keywords.append(",").append("батарея");
                keywords.append(",").append("батарейка");
                keywords.append(",").append("акб");
                categoryUrl = "http://tiu.ru/Akkumulyatory-dlya-mobilnyh-telefonov-i-tsifrovoj-tehniki";
            } else {
                throw new IllegalStateException("Unknown category: " + categoryProps.get("category.engname"));
            }

            cell = new Label(column++, row, item.article);
            sheet.addCell(cell);
            cell = new Label(column++, row, itemProps.get("item.name"));
            sheet.addCell(cell);
            cell = new Label(column++, row, keywords.toString());
            sheet.addCell(cell);
            cell = new Label(column++, row, trim(itemProps.get("item.title"), 160));
            sheet.addCell(cell);
            cell = new Label(column++, row, trim(itemProps.get("item.title"), 10000));
            sheet.addCell(cell);
            cell = new Number(column++, row, item.price);
            sheet.addCell(cell);
            String pic = YandexMarket.BASE_PICTURE_URL + itemProps.get("item.photo").split(";")[0].split(",")[2].replaceAll(" ", "%20");
            cell = new Label(column++, row, pic);
            sheet.addCell(cell);
            cell = new Label(column++, row, categoryUrl);
            sheet.addCell(cell);
            String presence = item.count > 0 ? "+" : "-";
            cell = new Label(column++, row, presence);
            sheet.addCell(cell);
            cell = new Label(column++, row, "u");
            sheet.addCell(cell);
            cell = new Label(column++, row, "шт.");
            sheet.addCell(cell);
            cell = new Label(column++, row, "руб.");
            sheet.addCell(cell);
            if (item.models != null && item.models.size() > 0) {
                for (String model : item.models) {
                    templates.put("\\$\\{model\\.name\\}", item.brand + " " + model);
                    Map<String, String> itemModelProps = DataHelper.readProps(DataHelper.Props.ITEM_MODEL, propsFile);
                    itemModelProps = ExtramobiNew.filterProps(itemModelProps, templates);
                    ExtramobiNew.filterProp(itemModelProps, "item.model.name", "\\$\\{item\\.mah\\}", ExtramobiNew.getMah(item.props));
                    column = 0;
                    ++row;
                    cell = new Label(column++, row, item.article);
                    sheet.addCell(cell);
                    cell = new Label(column++, row, itemModelProps.get("item.model.name"));
                    sheet.addCell(cell);
                    cell = new Label(column++, row, keywords.toString());
                    sheet.addCell(cell);
                    cell = new Label(column++, row, trim(itemModelProps.get("item.model.title"), 160));
                    sheet.addCell(cell);
                    cell = new Label(column++, row, trim(itemModelProps.get("item.model.title"), 10000));
                    sheet.addCell(cell);
                    cell = new Number(column++, row, item.price);
                    sheet.addCell(cell);
                    cell = new Label(column++, row, pic);
                    sheet.addCell(cell);
                    cell = new Label(column++, row, categoryUrl);
                    sheet.addCell(cell);
                    cell = new Label(column++, row, presence);
                    sheet.addCell(cell);
                    cell = new Label(column++, row, "u");
                    sheet.addCell(cell);
                    cell = new Label(column++, row, "шт.");
                    sheet.addCell(cell);
                    cell = new Label(column++, row, "руб.");
                    sheet.addCell(cell);
                }
            }
        }
        workbook.write();
        workbook.close();
    }

    private static String trim(String desc, int symbols) {
        desc = desc.trim();
        if (desc.length() > symbols) {
            desc = desc.substring(0, symbols - 1);
        }
        return desc;
    }
}
